
#include "project.h"
#include "ports.h"
#include "display.h"

#include "backlight.h"


void sx_backlight(u8 line) {
	if (sButton.backlight_status != 2) {
		sButton.backlight_status = 2;
		sButton.backlight_timeout = 0;
		P2OUT |= BUTTON_BACKLIGHT_PIN;
        P2DIR |= BUTTON_BACKLIGHT_PIN;
        display_symbol(LCD_SYMB_PM, SEG_ON);
	}
	else {
		sButton.backlight_status = 0;
		P2OUT &= ~BUTTON_BACKLIGHT_PIN;
        P2DIR &= ~BUTTON_BACKLIGHT_PIN;
        display_symbol(LCD_SYMB_PM, SEG_OFF);
	} 
}



void display_backlight(u8 line, u8 update){
	if (sButton.backlight_status == 2) {
		display_chars(LCD_SEG_L2_4_0, (u8 *) "BL ON", SEG_ON);
	}
	else {
		display_chars(LCD_SEG_L2_4_0, (u8 *) "BLOFF", SEG_ON);
	}
}


// called in timer.c every 1 second if backlight is on
void backlight_timeout() {
		
	if (sButton.backlight_timeout > BACKLIGHT_TIMEOUT) {
		sButton.backlight_status = 0;
		P2OUT &= ~BUTTON_BACKLIGHT_PIN;
    	P2DIR &= ~BUTTON_BACKLIGHT_PIN;
    	display_symbol(LCD_SYMB_PM, SEG_OFF);
	}
	else {
		sButton.backlight_timeout++;
	}
	
}
